import { Inject, Injectable, Logger } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager'
import { Cache } from 'cache-manager';
import {
    getAppToken,
    getUserAccessToken,
    getUserToken,
    refreshUserToken,
} from 'src/helper/feishu/auth';
import { BusinessException } from '@/common/exceptions/business.exception.filter';
import { ConfigService } from '@nestjs/config';
import { messages } from '@/helper/feishu/message';

@Injectable()
export class FeishuService {

    private APP_TOKEN_CACHE_KEY;

    constructor(
        @Inject(CACHE_MANAGER) private cacheManager: Cache,
        private configService: ConfigService,
    ) {
        this.APP_TOKEN_CACHE_KEY = this.configService.get('APP_TOKEN_CACHE_KEY')
    }
    // constructor(
    //     @Inject(CACHE_MANAGER) private cacheManager: Cache,
    // ) {
    //     this.APP_TOKEN_CACHE_KEY = this.configService.get('APP_TOKEN_CACHE_KEY')
    // }


    async getAppToken() {
        let appToken: string | undefined;
        appToken = await this.cacheManager.get(this.APP_TOKEN_CACHE_KEY);

        this.cacheManager.set('APP_TOKEN_CACHE_KEY', 'hello', 600000);
        if (!appToken) {
            const response = await getAppToken();

            const ttl = response.expire - 60;
            if (response.code === 0) {
                // token 有效期为 2 小时，在此期间调用该接口 token 不会改变。当 token 有效期小于 30 分的时候,再次请求获取 token 的时候，会生成一个新的 token，与此同时老的 token 依然有效。
                appToken = response.app_access_token;
                this.cacheManager.set(this.APP_TOKEN_CACHE_KEY, appToken, ttl * 1000);
            } else {
                throw new BusinessException('飞书调用异常')
            }
        }
        return appToken;
    }

    async sendMessage(receive_id_type: any, params: any) {
        const app_token = await this.getAppToken()
        console.log('--sendMessage: ', app_token);

        return messages(receive_id_type, params, app_token as string)
    }

    // async sendOk() {
    //     this.cacheManager.set('feishuService', 'sendMessage sendOk', 600000);
    //     console.log('---ftf');


    //     return 'sendOk'
    // }
}